﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Model;
using MySql.Data.MySqlClient;

namespace DAL
{
    public  class merchant_activityDal
    {
        /// <summary>
        /// 通过类型查活动
        /// </summary>
        /// <param name="activity_type"></param>
        /// <returns></returns>
        public static List<ms_merchant_activity> Querybytype(string activity_type)
        {
            string sql = "select * from ms_merchant_activity where activity_type=@activity_type";
            List<ms_merchant_activity> actlist = new List<ms_merchant_activity>();
            MySqlParameter[] values = new MySqlParameter[1];
            values[0] = new MySqlParameter("activity_type", activity_type);
            MySqlDataReader rd = DBHelper.ExcutQuery(sql, values);
            while (rd.Read())
            {
                ms_merchant_activity activity = new ms_merchant_activity();
                activity.id = rd["id"].ToString();
                activity.merchant_id = rd["merchant_id"].ToString();
                activity.merchant_name = rd["merchant_name"].ToString();
                activity.activity_type = rd["activity_type"].ToString();
                activity.province = rd["province"].ToString();
                activity.longitude = rd["longitude"].ToString();
                activity.latitude = rd["latitude"].ToString();
                activity.start_time = rd["start_time"].ToString();
                activity.end_time = rd["end_time"].ToString();
                activity.goods_name = rd["goods_name"].ToString();
                activity.cover = rd["cover"].ToString();
                activity.carousel = rd["carousel"].ToString();
                activity.market_price = rd["market_price"].ToString();
                activity.describes = rd["describes"].ToString();
                activity.introduce = rd["introduce"].ToString();
                activity.discount_price = rd["discount_price"].ToString();
                activity.group_num = rd["group_num"].ToString();
                activity.stock = rd["stock"].ToString();
                activity.sale = rd["sale"].ToString();
                activity.audit_status = rd["audit_status"].ToString();
                activity.reason = rd["reason"].ToString();
                activity.add_time = rd["add_time"].ToString();
                activity.update_time = rd["update_time"].ToString();
                actlist.Add(activity);
            }
            rd.Close();
            return actlist;
        }
        /// <summary>
        /// 通过商户查商户活动
        /// </summary>
        /// <param name="merchant_id"></param>
        /// <returns></returns>
        public static List<ms_merchant_activity> Querybymerchantid(string merchant_id)
        {
            string sql = "select * from ms_merchant_activity where merchant_id=@merchant_id  order by update_time desc";
            List<ms_merchant_activity> actlist = new List<ms_merchant_activity>();
            MySqlParameter[] values = new MySqlParameter[1];
            values[0] = new MySqlParameter("merchant_id", merchant_id);
            MySqlDataReader rd = DBHelper.ExcutQuery(sql, values);
            while (rd.Read())
            {
                ms_merchant_activity activity = new ms_merchant_activity();
                activity.id = rd["id"].ToString();
                activity.merchant_id = rd["merchant_id"].ToString();
                activity.merchant_name = rd["merchant_name"].ToString();
                activity.activity_type = rd["activity_type"].ToString();
                activity.province = rd["province"].ToString();
                activity.longitude = rd["longitude"].ToString();
                activity.latitude = rd["latitude"].ToString();
                activity.start_time = rd["start_time"].ToString();
                activity.end_time = rd["end_time"].ToString();
                activity.goods_name = rd["goods_name"].ToString();
                activity.cover = rd["cover"].ToString();
                activity.carousel = rd["carousel"].ToString();
                activity.market_price = rd["market_price"].ToString();
                activity.describes = rd["describes"].ToString();
                activity.introduce = rd["introduce"].ToString();
                activity.discount_price = rd["discount_price"].ToString();
                activity.group_num = rd["group_num"].ToString();
                activity.stock = rd["stock"].ToString();
                activity.sale = rd["sale"].ToString();
                activity.audit_status = rd["audit_status"].ToString();
                activity.reason = rd["reason"].ToString();
                activity.add_time = rd["add_time"].ToString();
                activity.update_time = rd["update_time"].ToString();
                actlist.Add(activity);
            }
            rd.Close();
            return actlist;
        }
        /// <summary>
        /// 通过活动类型查商户活动
        /// </summary>
        /// <param name="merchant_id"></param>
        /// <param name="activity_type"></param>
        /// <returns></returns>
        public static List<ms_merchant_activity> Querybymerchantid(string merchant_id,string activity_type)
        {
            string sql = "select * from ms_merchant_activity where merchant_id=@merchant_id and activity_type=@activity_type order by update_time desc";
            List<ms_merchant_activity> actlist = new List<ms_merchant_activity>();
            MySqlParameter[] values = new MySqlParameter[2];
            values[0] = new MySqlParameter("merchant_id", merchant_id);
            values[1] = new MySqlParameter("activity_type", activity_type);
            MySqlDataReader rd = DBHelper.ExcutQuery(sql, values);
            while (rd.Read())
            {
                ms_merchant_activity activity = new ms_merchant_activity();
                activity.id = rd["id"].ToString();
                activity.merchant_id = rd["merchant_id"].ToString();
                activity.merchant_name = rd["merchant_name"].ToString();
                activity.activity_type = rd["activity_type"].ToString();
                activity.province = rd["province"].ToString();
                activity.longitude = rd["longitude"].ToString();
                activity.latitude = rd["latitude"].ToString();
                activity.start_time = rd["start_time"].ToString();
                activity.end_time = rd["end_time"].ToString();
                activity.goods_name = rd["goods_name"].ToString();
                activity.cover = rd["cover"].ToString();
                activity.carousel = rd["carousel"].ToString();
                activity.market_price = rd["market_price"].ToString();
                activity.describes = rd["describes"].ToString();
                activity.introduce = rd["introduce"].ToString();
                activity.discount_price = rd["discount_price"].ToString();
                activity.group_num = rd["group_num"].ToString();
                activity.stock = rd["stock"].ToString();
                activity.sale = rd["sale"].ToString();
                activity.audit_status = rd["audit_status"].ToString();
                activity.reason = rd["reason"].ToString();
                activity.add_time = rd["add_time"].ToString();
                activity.update_time = rd["update_time"].ToString();
                actlist.Add(activity);
            }
            rd.Close();
            return actlist;
        }
        /// <summary>
        /// 通过活动ID查活动详情
        /// </summary>
        /// <param name="actid"></param>
        /// <returns></returns>
        public static ms_merchant_activity Querybyactid(string actid)
        {
            string sql = "select * from ms_merchant_activity where id=@id";
            MySqlParameter[] values = new MySqlParameter[1];
            values[0] = new MySqlParameter("id", actid);
            MySqlDataReader rd = DBHelper.ExcutQuery(sql, values);
            ms_merchant_activity activity = new ms_merchant_activity();
            if (rd.Read())
            {
                activity.id = rd["id"].ToString();
                activity.merchant_id = rd["merchant_id"].ToString();
                activity.merchant_name = rd["merchant_name"].ToString();
                activity.activity_type = rd["activity_type"].ToString();
                activity.province = rd["province"].ToString();
                activity.longitude = rd["longitude"].ToString();
                activity.latitude = rd["latitude"].ToString();
                activity.start_time = rd["start_time"].ToString();
                activity.end_time = rd["end_time"].ToString();
                activity.goods_name = rd["goods_name"].ToString();
                activity.cover = rd["cover"].ToString();
                activity.carousel = rd["carousel"].ToString();
                activity.market_price = rd["market_price"].ToString();
                activity.describes = rd["describes"].ToString();
                activity.introduce = rd["introduce"].ToString();
                activity.discount_price = rd["discount_price"].ToString();
                activity.group_num = rd["group_num"].ToString();
                activity.stock = rd["stock"].ToString();
                activity.sale = rd["sale"].ToString();
                activity.audit_status = rd["audit_status"].ToString();
                activity.reason = rd["reason"].ToString();
                activity.add_time = rd["add_time"].ToString();
                activity.update_time = rd["update_time"].ToString();
                activity.u_sage = rd["u_sage"].ToString();
            }
            rd.Close();
            return activity;
        }
        /// <summary>
        /// 更新销量
        /// </summary>
        /// <param name="actid"></param>
        /// <param name="sale"></param>
        /// <returns></returns>
        public static int Updatesale(string actid, string sale)
        {
            string sql = "update ms_merchant_activity set sale=@sale where id=@id";
            MySqlParameter[] values = new MySqlParameter[2];
            values[0] = new MySqlParameter("id", actid);
            values[1] = new MySqlParameter("sale", sale);
            return DBHelper.ExcutUpdate(sql, values);
        }
        /// <summary>
        /// 添加活动
        /// </summary>
        /// <param name="activity"></param>
        /// <returns></returns>
        public static int Addactivity(ms_merchant_activity activity)
        {
            string sql = "insert into ms_merchant_activity(merchant_id,merchant_name,activity_type,province,longitude,latitude,start_time,often,unit,end_time,u_sage,goods_name,cover,carousel,market_price,describes,introduce,discount_price,stock,add_time) values(@merchant_id,@merchant_name,@activity_type,@province,@longitude,@latitude,@start_time,@often,@unit,@end_time,@u_sage,@goods_name,@cover,@carousel,@market_price,@describes,@introduce,@discount_price,@stock,@add_time)";
            MySqlParameter[] valeus = new MySqlParameter[20];
            valeus[0] = new MySqlParameter("merchant_id", activity.merchant_id);
            valeus[1] = new MySqlParameter("merchant_name", activity.merchant_name);
            valeus[2] = new MySqlParameter("activity_type", activity.activity_type);
            valeus[3] = new MySqlParameter("province", activity.province);
            valeus[4] = new MySqlParameter("longitude", activity.longitude);
            valeus[5] = new MySqlParameter("latitude", activity.latitude);
            valeus[6] = new MySqlParameter("start_time", activity.start_time);
            valeus[7] = new MySqlParameter("often", activity.often);
            valeus[8] = new MySqlParameter("unit", activity.unit);
            valeus[9] = new MySqlParameter("end_time", activity.end_time);
            valeus[10] = new MySqlParameter("u_sage", activity.u_sage);
            valeus[11] = new MySqlParameter("goods_name", activity.goods_name);
            valeus[12] = new MySqlParameter("cover", activity.cover);
            valeus[13] = new MySqlParameter("carousel", activity.carousel);
            valeus[14] = new MySqlParameter("market_price", activity.market_price);
            valeus[15] = new MySqlParameter("describes", activity.describes);
            valeus[16] = new MySqlParameter("introduce", activity.introduce);
            valeus[17] = new MySqlParameter("discount_price", activity.discount_price);
            valeus[18] = new MySqlParameter("stock", activity.stock);
            valeus[19] = new MySqlParameter("add_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
            return DBHelper.ExcutUpdate(sql, valeus);
        }
        /// <summary>
        /// 更新活动
        /// </summary>
        /// <param name="activity"></param>
        /// <param name="actvid"></param>
        /// <returns></returns>
        public static int Updateactvitybyid(ms_merchant_activity activity, string actvid)
        {
            string sql = "update ms_merchant_activity set activity_type=@activity_type,start_time=@start_time,end_time=@end_time,u_sage=@u_sage,goods_name=@goods_name,cover=@cover,carousel=@carousel,market_price=@market_price,describes=@describes,discount_price=@discount_price,stock=@stock,audit_status=1 where id=@id";
            MySqlParameter[] valeus = new MySqlParameter[13];
            valeus[0] = new MySqlParameter("activity_type", activity.activity_type);
            valeus[1] = new MySqlParameter("start_time", activity.start_time);
            valeus[2] = new MySqlParameter("end_time", activity.end_time);
            valeus[3] = new MySqlParameter("u_sage", activity.u_sage);
            valeus[4] = new MySqlParameter("goods_name", activity.goods_name);
            valeus[5] = new MySqlParameter("cover", activity.cover);
            valeus[6] = new MySqlParameter("carousel", activity.carousel);
            valeus[7] = new MySqlParameter("market_price", activity.market_price);
            valeus[8] = new MySqlParameter("describes", activity.describes);
            valeus[9] = new MySqlParameter("introduce", activity.introduce);
            valeus[10] = new MySqlParameter("discount_price", activity.discount_price);
            valeus[11] = new MySqlParameter("stock", activity.stock);
            valeus[12] = new MySqlParameter("id", actvid);
            return DBHelper.ExcutUpdate(sql, valeus);
        }
    }
}
